//
//  CLWebDataView.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2023/3/21.
//

#import "CLWebDataView.h"
@interface CLWebDataView()

@end
@implementation CLWebDataView

static CLWebDataView *instant = nil;
+ (CLWebDataView *)shared {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instant = [[CLWebDataView alloc] initWithFrame:SCREENBOUNDS];
        CLWebView *webView = [[CLWebView alloc] initWithFrame:instant.bounds];
        webView.isMain = YES;
        [instant addSubview:webView];
        instant.webView = webView;
        
        NSString *path = [[NSBundle mainBundle]pathForResource:@"pass.js" ofType:nil];
        __block NSString *source = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        
        WKUserScript *script = [[WKUserScript alloc] initWithSource:source injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        [webView.configuration.userContentController addUserScript:script];

        [[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];
        UIStackView *stackView = [[UIStackView alloc]init];
        [instant addSubview:stackView];
        stackView.axis = UILayoutConstraintAxisHorizontal;
        stackView.alignment = UIStackViewAlignmentFill;
        stackView.distribution = UIStackViewDistributionFillEqually;
        
        [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.mas_equalTo(instant);
        }];
        
        NSArray *titles = @[@"后退",@"前进",@"刷新"];
        NSMutableArray *buttons = [NSMutableArray array];
        for (NSString *title in titles) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            [stackView addArrangedSubview:button];
            [button setTitle:title forState:UIControlStateNormal];
            [buttons addObject:button];
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(80);
            }];
            if ([title isEqualToString:@"后退"]) {
                if (@available(iOS 14.0, *)) {
                    [button addAction:[UIAction actionWithHandler:^(__kindof UIAction * _Nonnull action) {
                        [CLWebDataView.shared.webView goBack];
                    }] forControlEvents:UIControlEventTouchUpInside];
                }
            } else if ([title isEqualToString:@"前进"]) {
                if (@available(iOS 14.0, *)) {
                    [button addAction:[UIAction actionWithHandler:^(__kindof UIAction * _Nonnull action) {
                        [CLWebDataView.shared.webView goForward];
                    }] forControlEvents:UIControlEventTouchUpInside];
                }
            } else {
                if (@available(iOS 14.0, *)) {
                    [button addAction:[UIAction actionWithHandler:^(__kindof UIAction * _Nonnull action) {
                        [CLWebDataView.shared.webView reload];
                    }] forControlEvents:UIControlEventTouchUpInside];
                }
            }
        }
    });
    return instant;
}

- (void)startWithUrl:(NSString *)url {
    [self.webView startWithUrl:url];
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    [super motionEnded:motion withEvent:event];
    if (self.superview == nil) {
        UIWindow *window = [CLSharedAppDelegate getRootWindow];
        [window addSubview:self];
        [self.webView reload];
    } else {
        [self removeFromSuperview];
    }
}

@end
